﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using ScribdNet = Scribd.Net;
using System.Xml;
using umbraco;
using System.Xml.XPath;
using TheOutfield.UmbExt.UniversalMediaPicker.Xslt;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Scribd
{
    public class ScribdXsltExtension : AbstractXsltExtension
    {
        public static XPathNodeIterator GetMedia(int docTypeDefinitionId, int id)
        {
            var provider = GetProvider<ScribdProvider>(docTypeDefinitionId);
            provider.EnsureScribdContext();

            try
            {
                var doc = ScribdNet.Document.Download(id);

                var xd = new XmlDocument();
                var docNode = xmlHelper.addTextNode(xd, "Document", "");

                if (doc != null)
                {
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Id", doc.DocumentId.ToString()));
                    docNode.AppendChild(xmlHelper.addCDataNode(xd, "Title", doc.Title));
                    docNode.AppendChild(xmlHelper.addCDataNode(xd, "Description", doc.Description));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Tags", doc.TagList != null && doc.TagList.Count > 0 ? string.Join(",", doc.TagList.ToArray()) : ""));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "LargeImageURL", doc.LargeImageURL != null ? doc.LargeImageURL.ToString() : ""));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailUrl", doc.ThumbnailUrl != null ? doc.ThumbnailUrl.ToString() : ""));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Url", ScribdNet.Document.GetDownloadURL(id, "original")));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "CategoryId", doc.CategoryId.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "DocumentType", doc.DocumentType));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "PageCount", doc.PageCount.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Edition", doc.Edition));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "RevisionId", doc.RevisionId.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "License", doc.License.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Author", doc.Author));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Publisher", doc.Publisher));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "WhenPublished", doc.WhenPublished.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "SecretPassword", doc.SecretPassword));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "AccessKey", doc.AccessKey));
                    docNode.AppendChild(xmlHelper.addCDataNode(xd, "EmbedCode", doc.GetEmbedCode()));
                }

                xd.AppendChild(docNode);

                return xd.CreateNavigator().Select("/Document");
            }
            catch (Exception e)
            {
                return e.ToXPathNodeIterator();
            }
        }

        public static string GetEmbedCode(int docTypeDefinitionId, int id, int startingPage, string viewMode, string viewWidth, string viewHeight)
        {
            var provider = GetProvider<ScribdProvider>(docTypeDefinitionId);
            provider.EnsureScribdContext();

            var doc = ScribdNet.Document.Download(id);
            var scribdViewMode = (ScribdNet.ViewMode)Enum.Parse(typeof(ScribdNet.ViewMode), viewMode);

            return ScribdNet.Document.GetEmbedCode(doc, startingPage, scribdViewMode, viewHeight, viewWidth);
        }
    }
}